package com.example.demo.service.impl.proxy;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author chenbenfeng
 * @date 2021-04-12
 */
public class TestMain {
    public static final String DATE_FORMAT_yy_MM_dd3 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_yy_MM_dd4 = "yyyy/MM/dd";

    public static void main(String[] args) {
        /*UserImpl user = new UserImpl();
        // 代理角色
        ProxyInvocationHandler handler = new ProxyInvocationHandler();
        // 设置代理对象
        handler.setTarget(user);
        // 动态生成代理类
        UserService proxy = (UserService) handler.getProxy();
        proxy.query();*/

       /* Date s = parseDate("2021-11-02", "yyyy-MM-dd");
        String start = dateFormat(s, "yyyy/MM/dd");
        System.out.println(s);
        System.out.println(start);

        Date s1 = parseDate("2021/11/02", "yyyy/MM");
        String start2 = dateFormat(s1, "yyyy/MM");
        System.out.println(s1);
        System.out.println(start2);*/

        /*BigDecimal multiply = new BigDecimal(10 - 26).divide(new BigDecimal(26), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
        BigDecimal multiply2 = new BigDecimal(10 - 26).multiply(new BigDecimal(100)).divide(new BigDecimal(26), 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(multiply);
        System.out.println(multiply2);

        System.out.println(countRate(new BigDecimal(10), new BigDecimal(20)));*/

        /*Date date = addDay(new Date(), -7);

        long dateSubtract = getDateSubtractDay(new Date(), date);
        System.out.println(dateSubtract);*/

       /* List<Integer> test = new ArrayList<>();
        test.add(1);
        test.add(2);
        test.add(3);
        test.add(4);
        test.add(5);
        test.add(6);
        test.add(7);
        test.add(8);
        test.add(9);
        test.add(10);
        test.add(11);
        test.add(12);
        test.add(13);
        test.add(14);
        // test.add(15);

        for (int i = 0; i < 12; i++) {
            if (test.size() >= i*7 + 7) {
                System.out.println(test.subList(i*7, i*7 + 7).toString());
            }else {
                if (i*7 >= test.size()) {
                    break;
                }
                System.out.println(test.subList(i*7, test.size()).toString());
            }

        }*/

        /*System.out.println(getPastMontFirstDay(0));
        System.out.println(getPastMontLastDay(0));
        for (int i = 5; i >= 0; i--) {
            System.out.println(i);
            System.out.println(getPastMontFirstDay(i));
            System.out.println(getPastMontLastDay(i));
        }

        String s = dateFormat(new Date(), "yyyy/MM");
        System.out.println(s);*/

        Date s = parseDate("2021-03", "yyyy-MM");
        System.out.println(s);

        Date preMonth = getPreMonth(s, 1);
        System.out.println(preMonth);

        // 获取两个月前
        Date date = getPassMonthDate(new Date(), 2);
        // 格式化 yyyy-MM
        String dateFormat = dateFormat(date, "yyyy-MM");
        System.out.println(dateFormat);


        List<String> list = new ArrayList<>();
        list.add("11");
        list.add("12");
        list.add("13");
        list.add("14");
        list.add("15");
        list.add("16");

        List<String> strings = list.subList(0, 2);
        System.out.println(strings);
        List<String> strings1 = list.subList(2, 4);
        System.out.println(strings1);
        List<String> strings2 = list.subList(4, 6);
        System.out.println(strings2);


        BigDecimal divide = new BigDecimal("827").divide(new BigDecimal("100"), 0, BigDecimal.ROUND_DOWN);
        BigDecimal divide2 = new BigDecimal("827").divide(new BigDecimal("100"), 0, BigDecimal.ROUND_UP);
        System.out.println(divide);
        System.out.println(divide2);

        BigDecimal divide11 = new BigDecimal("850").divide(new BigDecimal("100"), 0, BigDecimal.ROUND_DOWN);
        BigDecimal divide112 = new BigDecimal("850").divide(new BigDecimal("100"), 0, BigDecimal.ROUND_UP);
        System.out.println(divide11);
        System.out.println(divide112);

        BigDecimal divide1 = new BigDecimal("857").divide(new BigDecimal("100"), 0, BigDecimal.ROUND_DOWN);
        BigDecimal divide12 = new BigDecimal("857").divide(new BigDecimal("100"), 0, BigDecimal.ROUND_UP);
        System.out.println(divide1);
        System.out.println(divide12);
    }

    /**
     * 获取任意时间的上n个月的第一天
     */
    public static Date getPassMonthDate(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - n);
        return calendar.getTime();
    }

    /**
     * 获取任意时间的上一个月
     * 描述:<描述函数实现的功能>.
     */
    public static Date getPreMonth(Date date, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - n);
        return calendar.getTime();
    }

    public static String dateFormat(Date date, String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    public static Date getPastMontFirstDay(int pastMonth) {
        //获取前一个月第一天
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, - pastMonth);
        calendar1.set(Calendar.DAY_OF_MONTH,1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        return parseDate(dft.format(calendar1.getTime()), "yyyy-MM-dd");
    }


    public static Date getPastMontLastDay(int pastMonth) {
        //获取前一个月最后一天
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.MONTH, - pastMonth);
        calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        return parseDate(dft.format(calendar2.getTime()), "yyyy-MM-dd");
    }

    public static long getDateSubtractDay(Date beginDate, Date endDate) {
        long subtractDate = 0L ;
        try {
            subtractDate = Math.toIntExact((beginDate.getTime() - endDate.getTime()) / (3600000L * 24));
        } catch (Exception e) {
            return 0 ;
        }
        return subtractDate;
    }

    public static long getDateSubtract(Date beginDate, Date endDate) {
        long subtractDate = 0L;
        try {
            subtractDate = (beginDate.getTime() - endDate.getTime()) / 3600000L;
        } catch (Exception e) {
            return 0L;
        }
        return subtractDate;
    }

    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, day);
        return calendar.getTime();
    }

    /**
     *  计算同比
     */
    public static BigDecimal countRate(BigDecimal count1, BigDecimal lastCount2) {
        return count1.subtract(lastCount2).multiply(new BigDecimal(100)).divide(lastCount2, 2, BigDecimal.ROUND_HALF_UP);
    }

    public static Date parseDate(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
